package com.qianfeng.util;

import com.qianfeng.service.QianFengService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * 2019/12/9
 * Administrator
 * springboot1207
 * 面向对象面向君  不负代码不负卿
 */

@Component
public class MyRealm  extends AuthorizingRealm {

    @Autowired
    private QianFengService qianFengService;


    @Override  //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1.得到认证通过以后的用户信息
        String  uname = (String)principalCollection.getPrimaryPrincipal();
        //2.通过用户信息查询该用户对应的权限列表
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRole("管理员");
        //权限集合
        ArrayList arrayList = new ArrayList();
        arrayList.add("user:select");
        arrayList.add("user:update");
        authorizationInfo.addStringPermissions(arrayList);
        return authorizationInfo;
    }

    @Override   //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       //1.获取用户提交的用户名
        String  username = (String) authenticationToken.getPrincipal();
        System.out.println("QianFengServiceImpl="+qianFengService);
        //2.查询数据库
        if("admin".equals(username)){
             //用户名正确  参数:用户信息,密码，盐值,自定义名称
            return  new SimpleAuthenticationInfo(username,"1234","myrealm");
        }
        return null;
    }
}
